package com.qiantu.phone.ui.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.entity.SceneDetailBean;
import com.qiantu.api.utils.ThemeUtil;
import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.glide.GlideApp;
import com.qiantu.phone.ui.activity.EditSceneBaseActivity;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class SceneIntentDetailAdapter extends SceneDetailBaseAdapter {

    public SceneIntentDetailAdapter(EditSceneBaseActivity activity, SceneDetailBean mSceneDetailBean){
        super(activity, mSceneDetailBean);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TOP_VIEW_TYPE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_edit_scene_intent_top, parent, false);
            return new TopItemViewHolder(view);
        } else if (viewType == DELETE_VIEW_TYPE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_delete_btn, parent, false);
            return new DeleteItemViewHolder(view);
        } else {
            return super.onCreateViewHolder(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List payloads) {
        if (!payloads.isEmpty()) {
            String payload = (String) payloads.get(0);
            switch (payload) {
                case "updateName":
                    updateName((TopItemViewHolder) holder);
                    break;
                case "updateIcon":
                    updateIcon((TopItemViewHolder) holder);
                    break;
            }
        }else{
            onBindViewHolder(holder, position);
        }
    }

    private void updateName(TopItemViewHolder topItemViewHolder){
        //更新场景名称
        topItemViewHolder.setName.setRightText(mSceneDetailBean.getName());
    }

    private void updateIcon(TopItemViewHolder topItemViewHolder){
        //更新场景图片
        String image = mSceneDetailBean.getImage();
        if (TextUtils.isEmpty(image)) {
            topItemViewHolder.sceneIcon.setVisibility(View.GONE);
            topItemViewHolder.selectIcon.setRightText("");
        } else {
            topItemViewHolder.sceneIcon.setVisibility(View.VISIBLE);
            GlideApp.with(mEditSceneActivity)
                    .load(BuildConfig.APP_SCENE_BASE + ThemeUtil.getIconThemeSuffix() + image + ".png")
                    //.placeholder(R.drawable.avatar_placeholder_ic)
                    //.error(R.drawable.avatar_placeholder_ic)
                    .into(topItemViewHolder.sceneIcon);
            topItemViewHolder.selectIcon.getRightView().setText("  ");
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        super.onBindViewHolder(viewHolder, position);
    }

    private class TopItemViewHolder extends RecyclerView.ViewHolder {
        private SettingBar setName;
        private SettingBar selectIcon;
        private ImageView sceneIcon;

        public TopItemViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            setName = itemView.findViewById(R.id.set_name);
            selectIcon = itemView.findViewById(R.id.select_icon);
            sceneIcon = itemView.findViewById(R.id.scene_icon);

            updateName(this);
            updateIcon(this);
            setName.setOnClickListener(topBottomViewOnClickListener);
            selectIcon.setOnClickListener(topBottomViewOnClickListener);
            sceneIcon.setOnClickListener(topBottomViewOnClickListener);
        }
    }
}
